﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace Smatime.Web.Models
{
    public class Company : BaseEntity
    {

        public long UserAccessId { get; set; }
        public string Name { get; set; }
        public string SiteName { get; set; }
        public int IsOrganization { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public int PostCode { get; set; }
        public string City { get; set; }
        public string District { get; set; }
        public string Address { get; set; }
        public string BuildingName { get; set; }
        public override BaseEntity FromDataRow(DataRow row)
        {
            var listListProperties = GetType().GetProperties()
                .Where(
                    propertyInfo => (row.Table.Columns.Contains(propertyInfo.Name)) &&
                                    (row[propertyInfo.Name].HasValue()));
            foreach (var propertyInfo in listListProperties)
            {
                propertyInfo.SetValue(this, row[propertyInfo.Name]);
            }
            return base.FromDataRow(row);
        }

        public override void ToDataRow(DataRow row)
        {
            var listListProperties = GetType().GetProperties()
                .Where(propertyInfo =>
                    (propertyInfo.GetValue(this).HasValue()));
            foreach (var propertyInfo in listListProperties)
            {
                row[propertyInfo.Name] = propertyInfo.GetValue(this);
            }
            base.ToDataRow(row);
        }
        //public override BaseEntity FromDataRow(DataRow row)
        //{
        //    Id = (long) row["Id"];
        //    UserAccessId = (long) row["UserAccessId"];
        //    Name = row["Name"].ToString();
        //    SiteName = row["SiteName"].ToString();
        //    IsOrganization = (int) row["IsOrganization"];
        //    Phone = row["Phone"].ToString();
        //    Fax = row["Fax"].ToString();
        //    PostCode = (int) row["PostCode"];
        //    City = row["City"].ToString();
        //    District = row["District"].ToString();
        //    Address = row["Address"].ToString();
        //    BuildingName = row["BuildingName"].ToString();
        //    return base.FromDataRow(row);
        //}

        //public override void ToDataRow(DataRow row)
        //{
        //    row["UserAccessId"] = UserAccessId;
        //    row["Name"] = Name;
        //    row["SiteName"] = SiteName;
        //    row["IsOrganization"] = IsOrganization;
        //    row["Phone"] = Phone;
        //    row["Fax"] = Fax;
        //    row["PostCode"] = PostCode;
        //    row["City"] = City;
        //    row["District"] = District;
        //    row["Address"] = Address;
        //    row["BuildingName"] = BuildingName;

        //    base.ToDataRow(row);
        //}
    }
}